package io.renren.common.weixin;

import io.renren.common.utils.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Created by whc on 2015/10/29.
 */
@Component
public class Message {

    @Value("${weixin.appId}")
    private String appId;
    @Value("${weixin.appSecret}")
    private String appSecret;


    @Autowired
    private WeiXinAccessTokenService weiXinAccessTokenService;

    public String getAccess_token(String newAppId,String newAppSecret) {
        String access_token = weiXinAccessTokenService.getAccessToken(newAppId,newAppSecret);
        return access_token;
    }

    /**
     * 会员端发送消息
     * @param templateMessage
     * @return
     * @throws Exception
     */
    public String templateSend(WxMpTemplateMessage templateMessage) throws Exception {
        /*String id = templateMessage.getToUser();
        Customer customer = customerReadService.load(Long.parseLong(id)).getResult();
        Long memberId = customer.getMemberId();
        if(memberId!=null){
            return memberTemplateSend(memberId,templateMessage);
        }*/
        return null;
    }

    public String memberTemplateSend(Long memberId,WxMpTemplateMessage templateMessage){
        /*MemberThreeParty memberThreeParty = memberThreePartyReadService.queryWXByMemberId(memberId,appId);
        if(memberThreeParty!=null){
            templateMessage.setToUser(memberThreeParty.getThreeId());
            String result = send(templateMessage,appId,appSecret);
            return result;
        }*/
        return null;
    }

    /**
     * 商家发送消息
     * @param templateMessage
     * @return
     */
    public void businessTemplateSend(WxMpTemplateMessage templateMessage){
        /*String businessId = templateMessage.getToUser();
        BusinessUser businessUser = new BusinessUser();
        businessUser.setBusinessId(Long.parseLong(businessId));
        businessUser.setDelFlg(1);
        businessUser.setStatus(1);
        List<BusinessUser> lstBuser = businessUserReadService.getBusinessUser(businessUser).getResult();
        for(BusinessUser b:lstBuser){
            List<BusinessUserThreeParty> butpList = businessUserThreePartyReadService.queryByBusinessUserId(b.getId());
            for(BusinessUserThreeParty butp:butpList){
                String threeId = butp.getThreeId();
                if(threeId!=null&&!"".equals(threeId)) {
                    templateMessage.setToUser(threeId);
                    String result = send(templateMessage, businessAppId, businessAppSecret);
                    System.out.println(result);
                }
            }
        }*/
    }

    /**
     * 消息发送
     * @param templateMessage
     * @param newAppId
     * @param newAppSecret
     * @return
     */
    public String send(WxMpTemplateMessage templateMessage,String newAppId,String newAppSecret){
        String access_token = getAccess_token(newAppId,newAppSecret);
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+access_token;
        HttpClient client = new HttpClient();
        String result = client.sendPost(url, templateMessage.toJson());
        return result;
    }

    /*public static void main(String args[]){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
        WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
        templateMessage.setToUser("oj526wasiO5zjbWlVYSp8iyTdxqE");
        templateMessage.setTemplateId("pkR-E84UoKXQ5ZpzWuE8kkGfxVonxACsrX8ZChVeLy0");
        templateMessage.getDatas().add(new WxMpTemplateData("first", "尊敬的会员,【12】提醒您", "#000000"));
        templateMessage.getDatas().add(new WxMpTemplateData("keyword1", "", "#173177"));
        templateMessage.getDatas().add(new WxMpTemplateData("keyword2", sdf.format(new Date()) , "#173177"));
        templateMessage.getDatas().add(new WxMpTemplateData("remark", "\r\n 123", "#173177"));

        *//*String url = "https://api.weixin.qq.com/cgi-bin/token";
        String param = "grant_type=client_credential&appid=wxaf61ebbe3aeafe54&secret=61b68bf74e7acd15cf4b000a23db84f9";
        HttpClient client = new HttpClient();
        String result = client.sendGet(url, param);
        JSONObject jsonData = JSONObject.fromObject(result);
        String access_token = (String)jsonData.get("access_token");

        //String access_token = getAccess_token(newAppId,newAppSecret);
        String url2 = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+access_token;
        //HttpClient client = new HttpClient();
        String abc = client.sendPost(url2, templateMessage.toJson());*//*

        Message message = new Message();
        message.send(templateMessage,"wxaf61ebbe3aeafe54","61b68bf74e7acd15cf4b000a23db84f9");
    }*/

    /*public static void main(String args[]){
        //18005326752     18153239085   18561820666   13953288320  18954258666
        SMSUtil.sendSMS("15953295779", "【爱到家】突发！爱到家用户交互高潜转化订单超过300单，累计交易额突破100万！");
        SMSUtil.sendSMS("18005326752", "【爱到家】突发！爱到家用户交互高潜转化订单超过300单，累计交易额突破100万！");
        SMSUtil.sendSMS("18153239085", "【爱到家】突发！爱到家用户交互高潜转化订单超过300单，累计交易额突破100万！");
        SMSUtil.sendSMS("18561820666", "【爱到家】突发！爱到家用户交互高潜转化订单超过300单，累计交易额突破100万！");
        SMSUtil.sendSMS("13953288320", "【爱到家】突发！爱到家用户交互高潜转化订单超过300单，累计交易额突破100万！");
        SMSUtil.sendSMS("18954258666", "【爱到家】突发！爱到家用户交互高潜转化订单超过300单，累计交易额突破100万！");
    }*/

}
